#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;
using XcoAppSpaces.Contracts.CcrWiring;
using XcoAppSpaces.Contracts.Serialization;
using System.IO;

namespace XcoAppSpaces.Serialization.Binary
{
	/// <summary>
	/// Helper class for serialization and deserialization of objects (used in
    /// communication with remote spaces). Uses the BinaryFormatter.
	/// </summary>
	public class XcoBinarySerializer : IXcoSerializer
	{
		private readonly BinaryFormatter ser = new BinaryFormatter();

		/// <summary>
		/// Initializes the service.
		/// </summary>
		/// <param name="serviceRegistry">The registry with other services of the space instance.</param>
		public void Initialize(XcoAppSpaces.Contracts.Service.IXcoServiceRegistry serviceRegistry)
		{
			ser.SurrogateSelector = new SurrogateSelector(serviceRegistry.Resolve<ICcrWirer>());
		}

	    /// <summary>
		/// Serializes an object into a byte array.
		/// </summary>
		/// <param name="element">The object to be serialized.</param>
		/// <returns>The serialized object.</returns>
		public byte[] Serialize(object element)
		{
			using (var elementStream = new MemoryStream())
			{
				ser.Serialize(elementStream, element);
				return elementStream.GetBuffer();
			}
		}

		/// <summary>
		/// Deserializes an object from a byte array.
		/// </summary>
		/// <param name="element">The serialized object.</param>
		/// <returns>The object that has been deserialized.</returns>
		public object Deserialize(byte[] element)
		{
			using (var elementStream = new MemoryStream(element))
			{
				return ser.Deserialize(elementStream); 
			}
		}

		/// <summary>
		/// Checks if the given type can be serialized. In case of the binary serializer, types must be marked
		/// as serializable.
		/// </summary>
		/// <param name="type">The type to be checked.</param>
		/// <param name="errorInfo">Contains information about serialization problems, if the type cannot be serialized.</param>
		/// <returns>True if the type can be serialized, otherwise false.</returns>
		public bool CanTypeBeSerialized(Type type, out string errorInfo)
		{
			errorInfo = null;
			if (type.IsInterface) //interfaces cannot be validated because the concrete type is unknown - therefore always return true
			{
				return true;
			}
			if (!type.IsSerializable)
			{
				errorInfo = "The type " + type.FullName + " is not marked as serializable";
				return false;
			}
			return true;
		}
	}
}